package cn.action;

import cn.base.BaseAction;
import cn.domain.Privilege;
import cn.domain.Role;
import cn.domain.User;
import cn.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * Created by Administrator on 2017/3/23 0023.
 */
@Controller
@Scope("prototype")
public class PrivilegeAction extends BaseAction<Privilege> {
    private Long roleId;

    public Long getRoleId() {
        return roleId;
    }

    public void setRoleId(Long roleId) {
        this.roleId = roleId;
    }

    private Long[] privilegeId;

    public Long[] getPrivilegeId() {
        return privilegeId;
    }

    public void setPrivilegeId(Long[] privilegeId) {
        this.privilegeId = privilegeId;
    }

    /**
     * 跳转权限设置
     */
    public String setPrivilege() {
        /**
         * 1、根据id查询当前要设置的角色，用于回显
         * 2、查看所有权限数据，再页面展示
         * 3、查询当前觉得对应的权限，用于页面回显
         */
        //查询岗位
        Role role = roleService.getById(model.getId());
        getValueStack().push(role);
        //查询所有的权限
        //List<Privilege> list = privilegeService.findAll();
        List<Privilege> top=privilegeService.privilegeTop();
        getValueStack().set("privilege", top);
        //根据id做查询，用于会显数据
        Set<Privilege> set = role.getPrivileges();
        privilegeId = new Long[set.size()];
        int i = 0;
        //判断之前是否有权限
        if (set.size() > 0 || set != null) {
            for (Privilege set1 : set) {
                System.out.println(set1.getId());
                privilegeId[i++] = set1.getId();
            }
            getValueStack().push(privilegeId);
        }
        return "TiaoZhuan";
    }

    /**
     * 修改权限
     */
    public String set() {
        Role role = roleService.getById(roleId);
        if (privilegeId != null && privilegeId.length > 0) {
            List list = privilegeService.privilege(privilegeId);
            role.setPrivileges(new HashSet<Privilege>(privilegeService.privilege(privilegeId)));
        } else {
            role.setPrivileges(null);
        }
        roleService.update(role);
        return "Set";
    }
    public Set<Privilege> loginLeft(){
        Set<Privilege> privileges=null;
        User user= (User) ActionContext.getContext().getSession().get("user");
        Set<Role> role=user.getRole();
        for(Role roles :role){
           privileges= roles.getPrivileges();
        }
        return privileges;
    }
}
